package ferran.tomas.ta;

import java.util.ArrayList;

import ferran.tomas.dominio.Producto;
import ferran.tomas.dominio.User;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class Contacts extends Activity {

	private ServidorPrincipal sp;
	ListView lista;
	ArrayAdapter<String> adaptador;
	ArrayList<String> datos;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.contacts);
		sp = ServidorPrincipal.getInstancia();

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		

		ArrayList<String> ArrayContactos = User.getInstancia().getContactos();
		datos = new ArrayList<String>(ArrayContactos.size());

		int i = 1;

		for (String contacto : ArrayContactos) {

			datos.add(i + "  " + contacto);

			i++;

		}

		lista = (ListView) findViewById(R.id.ListView02);
		adaptador = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, datos);

		lista.setAdapter(adaptador);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		menu.setGroupVisible(R.id.grupo2, false);
		menu.setGroupVisible(R.id.grupo1, false);
		menu.setGroupVisible(R.id.grupo4, false);

		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {

		case R.id.add_contact:
			contact();
			return true;
		case R.id.back:
			back();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}

	public void back() {
		Intent intent = new Intent(Contacts.this, MainMenu.class);
		Contacts.this.startActivity(intent);
	}

	public void contact() {
		Intent intent = new Intent(Contacts.this, NewContact.class);
		Contacts.this.startActivity(intent);
	}
}
